﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using DevExpress.XtraEditors;

using QLTTAN.BLL;
using QLTTAN.DTO;
namespace QLTTAN.UserControls
{
    public partial class ucKetChuyenHocVien : UserControl
    {
        LopHocBLL LH_BLL;
        CT_LopHocBLL CT_LH_BLL;
        HocVienBLL HV_BLL;
        ThamSoBLL TS_BLL;

        int limit;

        public ucKetChuyenHocVien()
        {
            InitializeComponent();
        }

        private void FillDataInLookUpEditLopHoc()
        {
            try
            {
                DataTable dtLopHoc = LH_BLL.LopHoc_Load_MaxKhoaHoc();

                lookUpEditLopHoc1.Properties.DataSource = dtLopHoc;
                lookUpEditLopHoc1.Properties.DisplayMember = "TenLoaiLop";
                lookUpEditLopHoc1.Properties.ValueMember = "MaLop";

                lookUpEditLopHoc2.Properties.DataSource = dtLopHoc;
                lookUpEditLopHoc2.Properties.DisplayMember = "TenLoaiLop";
                lookUpEditLopHoc2.Properties.ValueMember = "MaLop";
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ucKetChuyenHocVien_Load(object sender, EventArgs e)
        {
            LH_BLL = new LopHocBLL();
            CT_LH_BLL = new CT_LopHocBLL();
            HV_BLL = new HocVienBLL();
            TS_BLL = new ThamSoBLL();

            FillDataInLookUpEditLopHoc();

            lookUpEditLopHoc1.Enabled = false;
            lookUpEditLopHoc2.Enabled = false;

            try
            {
                //Load số học viên tối đa của 1 lớp.
                limit = int.Parse(TS_BLL.ThamSo_Load().Rows[2][1].ToString());
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void LoadGridControlHocVien1()
        {

            try
            {
                if (cbxChon.SelectedItem.ToString() == "Học Viên Tiềm Năng")
                {
                    gridControlHocVien1.DataSource = HV_BLL.HocVien_LoadTiemNang();
                }
                else
                {
                    if (cbxChon.SelectedItem.ToString() == "Học Viên Chính Thức")
                    {
                        CT_LopHocDTO CT_LH_DTO = new CT_LopHocDTO();
                        CT_LH_DTO.MaLop = int.Parse(lookUpEditLopHoc1.EditValue.ToString());

                        gridControlHocVien1.DataSource = CT_LH_BLL.CT_LopHoc_Load(CT_LH_DTO);
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK);
            }     
        }

        private bool CheckError()
        {
            if (gridControlHocVien1.DataSource == null)
            {
                XtraMessageBox.Show("Bạn chưa chọn nơi chuyển đi. \t\n", "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return true;
            }

            if (lookUpEditLopHoc2.EditValue == null)
            {
                XtraMessageBox.Show("Bạn chưa chọn lớp học chuyển đến. \t\n", "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return true;
            }
            if (lookUpEditLopHoc1.EditValue != null && lookUpEditLopHoc1.EditValue.ToString() == lookUpEditLopHoc2.EditValue.ToString())
            {
                XtraMessageBox.Show("Trùng lớp học. \t\n", "Thông báo!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return true;
            }

            return false;
        }

        private void btnQuaPhaiTatCa_Click(object sender, EventArgs e)
        {
            if (CheckError())
                return;

            try
            {
                
                int remain = limit - gridViewHocVien2.RowCount;
                int rowcount = gridViewHocVien1.RowCount < remain ? gridViewHocVien1.RowCount : remain;

                for (int i = 0; i < rowcount; i++)
                {
                    DataRow row = gridViewHocVien1.GetDataRow(i);

                    CT_LopHocDTO CT_LH_DTO = new CT_LopHocDTO();
                    CT_LH_DTO.MaLop = int.Parse(lookUpEditLopHoc2.EditValue.ToString());
                    CT_LH_DTO.MaHV = int.Parse(row["MaHV"].ToString());

                    if (CT_LH_BLL.CT_LopHoc_Insert(CT_LH_DTO) <= 0)
                    {
                        XtraMessageBox.Show("Thêm học viên " + row["TenHV"].ToString() + " thất bại", "Thông báo!!!", MessageBoxButtons.OK);
                    }

                    if (lookUpEditLopHoc1.EditValue != null)
                    {
                        CT_LH_DTO.MaCTLH = int.Parse(row["MaCTLH"].ToString());
                        CT_LH_BLL.CT_LopHoc_Delete(CT_LH_DTO);
                    }
                }

                LoadGridControlHocVien1();

                CT_LopHocDTO CT_LH_DTO2 = new CT_LopHocDTO();
                CT_LH_DTO2.MaLop = int.Parse(lookUpEditLopHoc2.EditValue.ToString());

                gridControlHocVien2.DataSource = CT_LH_BLL.CT_LopHoc_Load(CT_LH_DTO2);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK);
            }
        }

        private void btnQuaPhai_Click(object sender, EventArgs e)
        {
            if (CheckError())
                return;

            try
            {
                // Load quy định sl học viên tối đa của 1 lớp vào.
                int remain = limit - gridViewHocVien2.RowCount;
                int rowcount = gridViewHocVien1.SelectedRowsCount < remain ? gridViewHocVien1.SelectedRowsCount : remain;

                int count = 0;
                foreach (int index in gridViewHocVien1.GetSelectedRows())
                {
                    if(count < rowcount)
                    {
                        DataRow row = gridViewHocVien1.GetDataRow(index);

                        CT_LopHocDTO CT_LH_DTO = new CT_LopHocDTO();
                        CT_LH_DTO.MaLop = int.Parse(lookUpEditLopHoc2.EditValue.ToString());
                        CT_LH_DTO.MaHV = int.Parse(row["MaHV"].ToString());

                        if (CT_LH_BLL.CT_LopHoc_Insert(CT_LH_DTO) <= 0)
                        {
                            XtraMessageBox.Show("Thêm học viên " + row["TenHV"].ToString() + " thất bại", "Thông báo!!!", MessageBoxButtons.OK);
                        }

                        if (lookUpEditLopHoc1.EditValue != null)
                        {
                            CT_LH_DTO.MaCTLH = int.Parse(row["MaCTLH"].ToString());
                            CT_LH_BLL.CT_LopHoc_Delete(CT_LH_DTO);
                        }
                    }

                    count++;
                }

                LoadGridControlHocVien1();

                CT_LopHocDTO CT_LH_DTO2 = new CT_LopHocDTO();
                CT_LH_DTO2.MaLop = int.Parse(lookUpEditLopHoc2.EditValue.ToString());

                gridControlHocVien2.DataSource = CT_LH_BLL.CT_LopHoc_Load(CT_LH_DTO2);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK);
            }
        }

        private void btnQuaTrai_Click(object sender, EventArgs e)
        {
            if (CheckError())
                return;

            try
            {
                CT_LopHocDTO CT_LH_DTO = new CT_LopHocDTO();

                int rowcount = gridViewHocVien2.SelectedRowsCount;

                if(lookUpEditLopHoc1.EditValue != null)
                {
                    // Load quy định.
                    int remain = limit - gridViewHocVien1.RowCount;
                    rowcount = gridViewHocVien2.SelectedRowsCount < remain ? gridViewHocVien2.SelectedRowsCount : remain;
                    CT_LH_DTO.MaLop = int.Parse(lookUpEditLopHoc1.EditValue.ToString());
                }
               
                int count = 0;
                foreach (int index in gridViewHocVien2.GetSelectedRows())
                {
                    if(count < rowcount)
                    {
                        DataRow row = gridViewHocVien2.GetDataRow(index);
                       
                        CT_LH_DTO.MaCTLH = int.Parse(row["MaCTLH"].ToString());

                        if (CT_LH_BLL.CT_LopHoc_Delete(CT_LH_DTO) <= 0)
                        {
                            XtraMessageBox.Show("Xóa học viên " + row["TenHV"].ToString() + " thất bại", "Thông báo!!!", MessageBoxButtons.OK);
                        }

                        if (lookUpEditLopHoc1.EditValue != null)
                        {
                            CT_LH_DTO.MaHV = int.Parse(row["MaHV"].ToString());
                            CT_LH_BLL.CT_LopHoc_Insert(CT_LH_DTO);
                        }
                    } 
                }

                LoadGridControlHocVien1();

                CT_LopHocDTO CT_LH_DTO2 = new CT_LopHocDTO();
                CT_LH_DTO2.MaLop = int.Parse(lookUpEditLopHoc2.EditValue.ToString());

                gridControlHocVien2.DataSource = CT_LH_BLL.CT_LopHoc_Load(CT_LH_DTO2);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK);
            }
        }

        private void btnQuaTraiTatCa_Click(object sender, EventArgs e)
        {
            if (CheckError())
                return;

            try
            {
                CT_LopHocDTO CT_LH_DTO = new CT_LopHocDTO();

                int rowcount = gridViewHocVien2.RowCount;
                // Load quy định tối đa.
                if (lookUpEditLopHoc1.EditValue != null)
                {
                    int remain = limit - gridViewHocVien1.RowCount;
                    rowcount = rowcount < remain ? rowcount : remain;
                    CT_LH_DTO.MaLop = int.Parse(lookUpEditLopHoc1.EditValue.ToString());
                }


                for (int i = 0; i < rowcount; i++)
                {
                    DataRow row = gridViewHocVien2.GetDataRow(i);
                    
                    CT_LH_DTO.MaCTLH = int.Parse(row["MaCTLH"].ToString());

                    if (CT_LH_BLL.CT_LopHoc_Delete(CT_LH_DTO) <= 0)
                    {
                        XtraMessageBox.Show("Xóa học viên " + row["TenHV"].ToString() + " thất bại", "Thông báo!!!", MessageBoxButtons.OK);
                    }

                    if (lookUpEditLopHoc1.EditValue != null)
                    {
                        CT_LH_DTO.MaHV = int.Parse(row["MaHV"].ToString());
                        CT_LH_BLL.CT_LopHoc_Insert(CT_LH_DTO);
                    }
                }

                LoadGridControlHocVien1();

                CT_LopHocDTO CT_LH_DTO2 = new CT_LopHocDTO();
                CT_LH_DTO2.MaLop = int.Parse(lookUpEditLopHoc2.EditValue.ToString());

                gridControlHocVien2.DataSource = CT_LH_BLL.CT_LopHoc_Load(CT_LH_DTO2);
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK);
            }
        }

        private void lookUpEditLopHoc1_EditValueChanged(object sender, EventArgs e)
        {
            if (lookUpEditLopHoc1.EditValue != null)
            {
                try
                {
                    CT_LopHocDTO CT_LH_DTO = new CT_LopHocDTO();
                    CT_LH_DTO.MaLop = int.Parse(lookUpEditLopHoc1.EditValue.ToString());

                    gridControlHocVien1.DataSource = CT_LH_BLL.CT_LopHoc_Load(CT_LH_DTO);
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK);
                }
            }
        }

        private void lookUpEditLopHoc2_EditValueChanged(object sender, EventArgs e)
        {
            if(lookUpEditLopHoc2.EditValue != null)
            {
                try
                {
                    CT_LopHocDTO CT_LH_DTO = new CT_LopHocDTO();
                    CT_LH_DTO.MaLop = int.Parse(lookUpEditLopHoc2.EditValue.ToString());

                    gridControlHocVien2.DataSource = CT_LH_BLL.CT_LopHoc_Load(CT_LH_DTO);
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK);
                }
            }
        }

        private void cbxChon_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxChon.SelectedItem.ToString() == "Học Viên Tiềm Năng")
            {
                lookUpEditLopHoc1.EditValue = null;

                lookUpEditLopHoc1.Enabled = false;

                lookUpEditLopHoc2.EditValue = null;

                lookUpEditLopHoc2.Enabled = true;

                try
                {
                    gridControlHocVien1.DataSource = HV_BLL.HocVien_LoadTiemNang();
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message, "Thông báo!!!", MessageBoxButtons.OK);
                }

                gridControlHocVien2.DataSource = null;
            }

            if (cbxChon.SelectedItem.ToString() == "Học Viên Chính Thức")
            {
                lookUpEditLopHoc1.Enabled = true;
                lookUpEditLopHoc2.Enabled = true;

                gridControlHocVien1.DataSource = null;
            }
        }

        private void gridViewHocVien1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "STT")
                e.DisplayText = (e.ListSourceRowIndex + 1).ToString();
        }

        private void gridViewHocVien2_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "STT")
                e.DisplayText = (e.ListSourceRowIndex + 1).ToString();
        }  
    }
}
